package com.tanhua.dubbo.service;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.MovementTimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.bson.types.ObjectId;

import java.util.List;

@Component
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存好友时间线数据
     * @param userId
     * @param movementId
     */
    @Async
    public void saveTimeLine(Long userId, ObjectId movementId){
        Criteria criteria = Criteria.where("userId").is(userId);

        Query query = Query.query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);

        for (Friend friend : friends) {
            MovementTimeLine line = new MovementTimeLine();
            line.setCreated(System.currentTimeMillis());
            line.setFriendId(friend.getFriendId());
            line.setMovementId(movementId);
            line.setUserId(friend.getUserId());
            mongoTemplate.save(line);
        }
    }
}
